<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- tab标签页 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <!-- 左边内容 -->
            <el-row style="height:60px">
              <el-button type="primary" size="small"><i class="el-icon-plus" @click="showDialog = true">新增角色</i></el-button>
            </el-row>
            <!-- 表格组件 -->
            <el-table border="" :data="list">
              <el-table-column type="index" label="序号" width="150" align="center" />
              <el-table-column label="名称" width="150" prop="name" align="center" />
              <el-table-column label="描述" prop="description" align="center" />
              <el-table-column label="操作" width="500" align="center">
                <!-- 作用域插槽 -->
                <template slot-scope="{ row }">
                  <el-button size="small" type="success" @click="assignProm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height:60px">
              <el-pagination
                :total="page.total"
                :page-size="page.pagesize"
                :current-page="page.page"
                background
                layout="prev, pager, next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-alert :closable="false" type="info" show-icon title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改" />
            <!-- 右边内容 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="企业名称" style="width:400px">
                <el-input v-model="fromDATa.name" disabled />
              </el-form-item>
              <el-form-item label="公司地址" style="width:400px">
                <el-input v-model="fromDATa.companyAddress" disabled />
              </el-form-item>
              <el-form-item label="公司电话" style="width:400px">
                <el-input v-model="fromDATa.companyPhone" disabled />
              </el-form-item>
              <el-form-item label="邮箱" style="width:400px">
                <el-input v-model="fromDATa.mailbox" disabled />
              </el-form-item>
              <el-form-item label="备注" style="width:400px">
                <el-input v-model="fromDATa.remarks" disabled type="textarea" :rows="3" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 放置一个弹层组件 -->
    <el-dialog title="编辑角色" :visible="showDialog" @close="canCel">
      <el-form ref="roleForm" label-width="120px" :model="roleForm" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 放置两个插槽 -->
      <el-row type="flex" justify="center">
        <el-col :span="5">
          <el-button @click="canCel">取消</el-button>
          <el-button type="primary" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 放置分配权限的弹层 -->
    <el-dialog title="分配权限" :visible="showPermDialog" @close="permCanCel">
      <!-- 权限是一颗树 将数据绑定到组件上-->
      <el-tree
        ref="permTree"
        :data="promData"
        :props="defaultProps"
        :default-expand-all="true"
        show-checkbox
        :check-strictly="true"
        node-key="id"
        :default-checked-keys="selectCheck"
      />
      <!-- id作为唯一标识 -->
      <!-- 确定取消 -->
      <el-row type="flex" justify="center">
        <el-col :span="5">
          <el-button @click="permCanCel">取消</el-button>
          <el-button type="primary" @click="permBtnOk">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      activeName: 'first',
      list: [], // 所有员工列表
      fromDATa: {},
      page: { // 分页数据
        page: 1,
        pagesize: 10,
        total: 0// 记录总数
      },
      showDialog: false, // 控制弹层
      roleForm: {
        name: '', // 角色名称
        description: '' // 角色描述
      },
      rules: {
        name: [{ required: true, message: '名字不能为空', trigger: 'blur' }]
      },
      showPermDialog: false, // 控制分配权限的显示隐藏
      promData: [], // 接受权限数据
      defaultProps: { // 显示字段的名称 和子属性字段的名称
        children: 'children',
        label: 'name'
      },
      roleId: null, // 当前记录分配权限的id
      selectCheck: [] // 用来记录权限点的标识.
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList() // 获取角色列表
    this.getCompanyInfo() // 获取企业数据
  },
  methods: {
    // 点击分配权限确认
    async permBtnOk() {
      // 调用el-tree方法 获取当前tree选择的数据 this.$refs.permTree.getCheckedNodes()
      console.log(this.$refs.permTree.getCheckedKeys())
      await assignPerm({ id: this.roleId, permIds: this.$refs.permTree.getCheckedKeys() })
      this.$message.success('操作成功')
      this.showPermDialog = false
    },
    // 分配权限弹出层 获取权限点数据
    async assignProm(id) {
      this.promData = tranListToTreeData(await getPermissionList(), '0')// 获取所以权限点 得到一个树形数据
      // 有id了 应该先记录下来
      const { permIds } = await getRoleDetail(id) // 获取这个ID权限的点数据
      this.selectCheck = permIds
      this.roleId = id
      this.showPermDialog = true
    },
    // 分配权限取消弹出层
    permCanCel() {
      this.showPermDialog = false
      this.selectCheck = [] // 重置数据
    },
    // 确认表单
    async btnOk() {
      try {
        await this.$refs.roleForm.validate()// 校验表单
        if (this.roleForm.id) {
          // 编辑角色
          await updateRole(this.roleForm)// 调用更新接口
        } else {
          // 新增角色
          await addRole(this.roleForm)
        }

        this.getRoleList() // 重新拉去数据
        this.showDialog = false // 关闭弹层
        this.$message.success('操作成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 取消表单
    canCel() {
      this.showDialog = false
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验规则
      this.$refs.roleForm.resetFields()
    },
    // 编辑角色
    async editRole(id) {
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true // 显示弹层
    },
    // 获取角色列表
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    // 获取企业数据
    async getCompanyInfo() {
      this.fromDATa = await getCompanyInfo(this.companyId)
    },
    // 点击删除角色
    async deleteRole(id) {
      try {
        await this.$confirm('确认删除该角色吗？')
        await deleteRole(id) // 调用删除接口
        this.getRoleList() // 重新加载数据
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    changePage(newPage) {
      // newPage当前点击的页码 将当前页面复制给最新的页码
      this.page.page = newPage
      this.getRoleList()
    }
  }
}
</script>

<style>

</style>
